package zhishu.iceyyy.com.bxq_2500.myadapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import zhishu.iceyyy.com.bxq_2500.R;
import zhishu.iceyyy.com.bxq_2500.activity.Work_Activity;
import zhishu.iceyyy.com.bxq_2500.base.Base_Z_Adapter;
import zhishu.iceyyy.com.bxq_2500.bean.MovielistD;
import zhishu.iceyyy.com.bxq_2500.bean.headerAdv;
import zhishu.iceyyy.com.bxq_2500.fragment.Zhi_Fragment;
import zhishu.iceyyy.com.bxq_2500.ui.MyGridView;


public class ListViewAdapter extends Base_Z_Adapter {
    private LayoutInflater mInflater;
    private Activity context;

    List<String> cchead = new ArrayList<String>();
    List<String> ccfoot = new ArrayList<String>();
    private GridViewAdapter nearByInfoImgsAdapter;


    public ListViewAdapter(Activity context) {
        super();
        this.mInflater = LayoutInflater.from(context);
        this.context = context;


    }


    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_listview, null);
            holder = new ViewHolder();
            holder.disName = (TextView) convertView.findViewById(R.id.info_tv_name);//昵称
            holder.rl4 = (RelativeLayout) convertView.findViewById(R.id.rl4);//图片布局
            holder.gv_images = (MyGridView) convertView.findViewById(R.id.gv_images);//图片
            holder.banner = (Banner) convertView.findViewById(R.id.banner1);//图片
            holder.banner2 = (Banner) convertView.findViewById(R.id.banner2);//图片
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MovielistD.DataBean.MovicesBean nrowDataeme = (MovielistD.DataBean.MovicesBean) getItem(position);
        List<MovielistD.DataBean.MovicesBean.MoviceBean> movice = nrowDataeme.getMovice();
        holder.disName.setText(nrowDataeme.getType().getType_name());
        initInfoImages(holder.gv_images, movice);
        if (nrowDataeme.getHeaderAdv() != null && nrowDataeme.getHeaderAdv().size() > 0) {
            holder.banner.setVisibility(View.VISIBLE);
            for (int i = 0; i < nrowDataeme.getHeaderAdv().size(); i++) {
                cchead.clear();
                cchead.add(nrowDataeme.getHeaderAdv().get(i).getMac_adv_show_url());
                startBanner(holder.banner, cchead);
            }


        }else {
            holder.banner.setVisibility(View.GONE);
        }
        if (nrowDataeme.getFooterAdv() != null && nrowDataeme.getFooterAdv().size() > 0) {
            holder.banner2.setVisibility(View.VISIBLE);
            ccfoot.clear();
            for (int i = 0; i < nrowDataeme.getFooterAdv().size(); i++) {
                ccfoot.add(nrowDataeme.getFooterAdv().get(i).getMac_adv_show_url());
                startBanner(holder.banner2, ccfoot);
            }

        }else {
            holder.banner2.setVisibility(View.GONE);
        }


        return convertView;
    }

    private void startBanner(Banner banner, List<String> imageUrl) {
        //设置banner样式(显示圆形指示器)
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置指示器位置（指示器居右）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(imageUrl);
        //设置banner动画效果
//        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
//        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(5000);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }


    static class ViewHolder {
        TextView disName;
        MyGridView gv_images;
        RelativeLayout rl4;
        Banner banner, banner2;
    }

    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //Glide 加载图片简单用法
            Glide.with(context).load(path).into(imageView);


        }


    }

    /**
     * 加载信息中包含的图片内容
     *
     * @param listimg
     */
    public void initInfoImages(final MyGridView gv_images, final List<MovielistD.DataBean.MovicesBean.MoviceBean> listimg) {


        nearByInfoImgsAdapter = new GridViewAdapter(context, listimg);
        gv_images.setAdapter(nearByInfoImgsAdapter);
        gv_images.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MovielistD.DataBean.MovicesBean.MoviceBean moviceBean = listimg.get(i);
                String vod_id = moviceBean.getVod_id();
                context.startActivity(new Intent(context, Work_Activity.class).putExtra("value", vod_id));
            }
        });

    }

}

